package top.chukongxiang.monitorservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import top.chukongxiang.monitorservice.components.SockjsPrincipal;
import top.chukongxiang.monitorservice.config.SockJsContextHolder;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/8/28 11:55
 */
@Controller
public class SocketJsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/getSessionId")
    public void id(Message<byte[]> message) {
        String sessionId = SimpMessageHeaderAccessor.wrap(message).getSessionId();
        SockjsPrincipal principal = SockJsContextHolder.getClientId(sessionId);
        if (principal != null && sessionId != null) {
            simpMessagingTemplate.convertAndSendToUser(principal.getName(), "/queue/getSessionId", sessionId);
        }
    }

}


